package com.tbit.uqbike.webmanager.controller;

import com.tbit.common.utils.SingUtil;
import com.tbit.uqbike.constant.OperaPswConstant;
import com.tbit.uqbike.object.pojo.dto.TransactionOrderIncomeDTO;
import com.tbit.uqbike.object.pojo.dto.TransactionOrderIncomeDownloadDTO;
import com.tbit.uqbike.object.pojo.dto.TransactionOrderIncomePageAppDTO;
import com.tbit.uqbike.object.pojo.dto.TransactionOrderIncomePageDTO;
import com.tbit.uqbike.object.pojo.vo.TransactionOrderIncomeVO;
import com.tbit.uqbike.object.pojo.vo.TransactionOrderTotalIncomeVO;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.TransactionOrderStatService;
import com.tbit.uqbike.webmanager.util.StringUtils;
import com.tbit.uqbike.webmanager.util.csv.annotations.AsyncDownload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 财务统计-商户交易订单统计
 *
 * @AUTHOR: wei.xing
 * @DATE: 2022-11-22 16:29
 **/
@RequestMapping("/transactionOrderStat")
@RestController
public class TransactionOrderStatController {

    @Autowired
    TransactionOrderStatService service;

    /**
     * 查询商户交易订单总收入
     *
     * @param dto
     * @return
     */
    @GetMapping("/getTotalIncome")
    public Result<TransactionOrderTotalIncomeVO> getTotalIncome(TransactionOrderIncomeDTO dto) {
        TransactionOrderTotalIncomeVO detailVO = service.getTotalIncome(dto);
        return Result.success(detailVO);
    }

    /**
     * 查询商户交易订单分页
     *
     * @param dto
     * @return
     */
    @GetMapping("/getIncomePage")
    public Result<PageResult<TransactionOrderIncomeVO>> getIncomePage(TransactionOrderIncomePageDTO dto) {
        PageResult<TransactionOrderIncomeVO> page = service.getIncomePage(dto);
        return Result.success(page);
    }

    /**
     * 查询商户交易订单分页（运维端调用）
     *
     * @param dto
     * @return
     */
    @GetMapping("/getIncomePageApp")
    public Result<PageResult<TransactionOrderIncomeVO>> getIncomePageApp(TransactionOrderIncomePageAppDTO dto) {
        if (StringUtils.isEmpty(dto.getSign()) || dto.getTime() == null) {
            return Result.error("校验参数有误");
        }
        if ((System.currentTimeMillis() - dto.getTime()) > 1000 * 60 * 5) {
            return Result.error("签名已过期");
        }
        if (!dto.getSign().equals(SingUtil.createSing(OperaPswConstant.ORDER_FINISH_MANUAL + dto.getTime()))) {
            return Result.error("签名有误");
        }
        if (dto.getPageNO() == null || dto.getRowCount() == null) {
            return Result.error("页码或条数为空");
        }
        PageResult<TransactionOrderIncomeVO> page = service.getIncomePage(dto);
        return Result.success(page);
    }


    /**
     * 商户交易订单的账单下载
     *
     * @param dto
     * @return
     */
    @AsyncDownload(defaultFileName = "商户交易订单统计")
    @PostMapping("/exportIncome")
    public Result exportIncome(@RequestBody TransactionOrderIncomeDownloadDTO dto) {
        String url = service.exportIncome(dto);
        return Result.success(url);
    }


}
